package bb.lanxing.lib.widget.indicator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import bb.lanxing.R;
import bb.lanxing.lib.widget.utils.Density;
import java.util.ArrayList;
import java.util.List;

public class PagerIndicator extends FrameLayout {
    private final float acceleration;
    private boolean autoDismiss;
    private final DataSetObserver dataSetObserver;
    private DotView dotView;
    private final Runnable fadeOutRun;
    private final float footMoveOffset;
    private final float headMoveOffset;
    private int indicatorColor;
    private int indicatorDefaultColor;
    private final TabItemLayoutChangedListener layoutChangedListener;
    private float radiusMax;
    private float radiusMin;
    private float radiusOffset;
    private LinearLayout tabContainer;
    private List<View> tabs;
    private ViewPager viewPager;


    public class TabItemLayoutChangedListener implements View.OnLayoutChangeListener {
        private int items;

        private TabItemLayoutChangedListener() {
        }

        @Override
        public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
            int childCount = ((ViewGroup) view).getChildCount();
            if (this.items != childCount) {
                int currentItem = PagerIndicator.this.viewPager.getCurrentItem();
                PagerIndicator.this.dotView.getHeadPoint().setX(PagerIndicator.this.getTabX(currentItem));
                PagerIndicator.this.dotView.getFootPoint().setX(PagerIndicator.this.getTabX(currentItem));
                PagerIndicator.this.dotView.getHeadPoint().setRadius(PagerIndicator.this.radiusMax);
                PagerIndicator.this.dotView.getFootPoint().setRadius(PagerIndicator.this.radiusMax);
                PagerIndicator.this.dotView.postInvalidate();
                this.items = childCount;
            }
        }

        public void clearItems() {
            this.items = 0;
        }
    }

    public PagerIndicator(Context context) {
        this(context, null);
    }

    public PagerIndicator(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.acceleration = 0.5f;
        this.headMoveOffset = 0.6f;
        this.footMoveOffset = 1.0f - 0.6f;
        this.indicatorDefaultColor = 0xFF999999;
        this.indicatorColor = 0xFF666666;

        this.fadeOutRun = new Runnable() {
            @Override
            public void run() {
                PagerIndicator.this.animate().setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animator) {
                        super.onAnimationEnd(animator);
                        PagerIndicator.this.setVisibility(View.INVISIBLE);
                    }
                }).alpha(0.0f).setDuration(200L).start();
            }
        };
        this.layoutChangedListener = new TabItemLayoutChangedListener();
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (PagerIndicator.this.tabContainer != null) {
                    PagerIndicator.this.tabContainer.removeAllViews();
                    PagerIndicator.this.addTabItems();
                    PagerIndicator.this.layoutChangedListener.clearItems();
                }
            }
        };
        initAttrs(context, attributeSet);
    }

    private void initAttrs(Context context, AttributeSet attributeSet) {
        this.radiusMax = Density.dp2px(context, 10.0f);
        this.radiusMin = Density.dp2px(context, 5.0f);
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, R.styleable.PagerIndicator);
        this.indicatorDefaultColor = obtainStyledAttributes.getColor(R.styleable.PagerIndicator_pager_indicator_default_color, this.indicatorDefaultColor);
        this.indicatorColor = obtainStyledAttributes.getColor(R.styleable.PagerIndicator_pager_indicator_color, this.indicatorColor);
        this.radiusMax = obtainStyledAttributes.getDimension(R.styleable.PagerIndicator_pager_indicator_maxRadius, this.radiusMax);
        this.radiusMin = obtainStyledAttributes.getDimension(R.styleable.PagerIndicator_pager_indicator_minRadius, this.radiusMin);
        this.autoDismiss = obtainStyledAttributes.getBoolean(R.styleable.PagerIndicator_pager_indicator_auto_dismiss, true);
        obtainStyledAttributes.recycle();
        this.radiusOffset = this.radiusMax - this.radiusMin;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        initSpringView();
        setUpListener();
    }

    private void initSpringView() {
        addTabContainerView();
        addTabItems();
        addPointView();
    }

    private void addPointView() {
        DotView dotView = new DotView(getContext());
        this.dotView = dotView;
        dotView.setIndicatorColor(this.indicatorColor);
        addView(this.dotView);
    }

    private void addTabContainerView() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        this.tabContainer = linearLayout;
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        this.tabContainer.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -1);
        layoutParams.gravity = Gravity.CENTER;
        addView(this.tabContainer, layoutParams);
    }

    public void addTabItems() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(this.radiusMin * 2.0f), Math.round(this.radiusMin * 2.0f), 1.0f);
        int i = (int) this.radiusMax;
        layoutParams.setMargins(i, i, i, i);
        this.tabs = new ArrayList();
        PagerAdapter adapter = this.viewPager.getAdapter();
        if (adapter == null) {
            return;
        }
        for (int i2 = 0; i2 < adapter.getCount(); i2++) {
            final int _i2 = i2;
            View view = new View(getContext());
            Drawable wrap = DrawableCompat.wrap(getContext().getResources().getDrawable(R.drawable.indicator_default_bg));
            DrawableCompat.setTint(wrap, this.indicatorDefaultColor);
            if (Build.VERSION.SDK_INT > 16) {
                view.setBackground(wrap);
            } else {
                view.setBackgroundDrawable(wrap);
            }
            view.setLayoutParams(layoutParams);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view2) {
                    PagerIndicator.this.viewPager.setCurrentItem(_i2);
                }
            });
            this.tabs.add(view);
            this.tabContainer.addView(view);
        }
    }

    private void createPoints() {
        View view = this.tabs.get(this.viewPager.getCurrentItem());
        float left = this.tabContainer.getLeft() + view.getX() + (view.getWidth() / 2);
        float top = this.tabContainer.getTop() + view.getY() + (view.getHeight() / 2);
        this.dotView.getHeadPoint().setX(left);
        this.dotView.getHeadPoint().setY(top);
        this.dotView.getFootPoint().setX(left);
        this.dotView.getFootPoint().setY(top);
        this.dotView.animCreate();
    }

    @Override
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        ViewPager viewPager;
        super.onLayout(z, i, i2, i3, i4);
        if (!z || (viewPager = this.viewPager) == null || viewPager.getAdapter() == null) {
            return;
        }
        createPoints();
        setSelectedTab(this.viewPager.getCurrentItem());
        if (!this.autoDismiss) {
            return;
        }
        if (this.viewPager.getAdapter().getCount() > 1) {
            postDelayed(this.fadeOutRun, 3000L);
        } else {
            setVisibility(View.INVISIBLE);
        }
    }

    private void setUpListener() {
        this.viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int i) {
                super.onPageSelected(i);
                PagerIndicator.this.setSelectedTab(i);
            }

            @Override
            public void onPageScrolled(int i, float f, int i2) {
                if (PagerIndicator.this.autoDismiss) {
                    if (PagerIndicator.this.getVisibility() != View.VISIBLE) {
                        PagerIndicator pagerIndicator = PagerIndicator.this;
                        pagerIndicator.removeCallbacks(pagerIndicator.fadeOutRun);
                        PagerIndicator.this.animate().setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationStart(Animator animator) {
                                super.onAnimationStart(animator);
                                PagerIndicator.this.setVisibility(View.VISIBLE);
                            }
                        }).alpha(1.0f).setDuration(200L).start();
                    }
                    if (f == 0.0f) {
                        PagerIndicator pagerIndicator2 = PagerIndicator.this;
                        pagerIndicator2.removeCallbacks(pagerIndicator2.fadeOutRun);
                        PagerIndicator pagerIndicator3 = PagerIndicator.this;
                        pagerIndicator3.postDelayed(pagerIndicator3.fadeOutRun, 1000);
                    }
                }
                if (i < PagerIndicator.this.tabs.size() - 1) {
                    int i3 = (f > 0.5f ? 1 : (f == 0.5f ? 0 : -1));
                    if (i3 < 0) {
                        PagerIndicator.this.dotView.getHeadPoint().setRadius(PagerIndicator.this.radiusMin);
                    } else {
                        PagerIndicator.this.dotView.getHeadPoint().setRadius((((f - 0.5f) / 0.5f) * PagerIndicator.this.radiusOffset) + PagerIndicator.this.radiusMin);
                    }
                    if (i3 < 0) {
                        PagerIndicator.this.dotView.getFootPoint().setRadius(((1.0f - (f / 0.5f)) * PagerIndicator.this.radiusOffset) + PagerIndicator.this.radiusMin);
                    } else {
                        PagerIndicator.this.dotView.getFootPoint().setRadius(PagerIndicator.this.radiusMin);
                    }
                    PagerIndicator.this.dotView.getHeadPoint().setX(PagerIndicator.this.getTabX(i) - ((f < PagerIndicator.this.headMoveOffset ? (float) ((Math.atan((((f / PagerIndicator.this.headMoveOffset) * PagerIndicator.this.acceleration) * 2.0f) - PagerIndicator.this.acceleration) + Math.atan(PagerIndicator.this.acceleration)) / (Math.atan(PagerIndicator.this.acceleration) * 2.0d)) : 1.0f) * PagerIndicator.this.getPositionDistance(i)));
                    PagerIndicator.this.dotView.getFootPoint().setX(PagerIndicator.this.getTabX(i) - ((f > PagerIndicator.this.footMoveOffset ? (float) ((Math.atan(((((f - PagerIndicator.this.footMoveOffset) / (1.0f - PagerIndicator.this.footMoveOffset)) * PagerIndicator.this.acceleration) * 2.0f) - PagerIndicator.this.acceleration) + Math.atan(PagerIndicator.this.acceleration)) / (Math.atan(PagerIndicator.this.acceleration) * 2.0d)) : 0.0f) * PagerIndicator.this.getPositionDistance(i)));
                    if (f == 0.0f) {
                        PagerIndicator.this.dotView.getHeadPoint().setRadius(PagerIndicator.this.radiusMax);
                        PagerIndicator.this.dotView.getFootPoint().setRadius(PagerIndicator.this.radiusMax);
                    }
                } else {
                    PagerIndicator.this.dotView.getHeadPoint().setX(PagerIndicator.this.getTabX(i));
                    PagerIndicator.this.dotView.getFootPoint().setX(PagerIndicator.this.getTabX(i));
                    PagerIndicator.this.dotView.getHeadPoint().setRadius(PagerIndicator.this.radiusMax);
                    PagerIndicator.this.dotView.getFootPoint().setRadius(PagerIndicator.this.radiusMax);
                }
                PagerIndicator.this.dotView.postInvalidate();
            }

            @Override
            public void onPageScrollStateChanged(int i) {
                super.onPageScrollStateChanged(i);
            }
        });
        PagerAdapter adapter = this.viewPager.getAdapter();
        if (adapter != null) {
            adapter.registerDataSetObserver(this.dataSetObserver);
        }
        this.viewPager.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() {
            @Override
            public void onAdapterChanged(ViewPager viewPager, PagerAdapter pagerAdapter, PagerAdapter pagerAdapter2) {
                if (pagerAdapter2 != null) {
                    pagerAdapter2.registerDataSetObserver(PagerIndicator.this.dataSetObserver);
                }
            }
        });
        this.tabContainer.addOnLayoutChangeListener(this.layoutChangedListener);
    }

    public float getPositionDistance(int i) {
        return this.tabs.get(i).getX() - this.tabs.get(i + 1).getX();
    }

    public float getTabX(int i) {
        return this.tabContainer.getLeft() + this.tabs.get(i).getX() + (this.tabs.get(i).getWidth() / 2);
    }

    public void setSelectedTab(int i) {
        for (View view : this.tabs) {
            view.setVisibility(View.VISIBLE);
        }
        this.tabs.get(i).setVisibility(View.INVISIBLE);
    }
}